package com.example.mystore.servlets.service;

import com.example.mystore.dao.OrderDao;
import com.example.mystore.entity.Commodity;
import com.example.mystore.entity.Customer;
import com.example.mystore.entity.Order;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class InsertOrderServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        声明订单
        Order order;
//        声明订单详细属性
        String customerEmail, consigneeName, consigneeAddress, consigneePAC, consigneePhone, consigneeFax, cardType, carName, cardNumber, billAddress,closingDate;
        double taxRate,freight, price;
        int commodityId,commodityNumber;
//        获得下单人的账号Email
        Customer customer = (Customer) req.getSession().getAttribute("customer");
        customerEmail= customer.getEmail();
//        默认税率为0.001
        taxRate = 0.001;
//        获得订单的地址支付信息
        HashMap<String ,String> orderMassage = (HashMap<String, String>) req.getSession().getAttribute("orderMassage");
//        获取购物车的订单
        HashMap<Commodity,Integer> cart = (HashMap<Commodity, Integer>) req.getSession().getAttribute("cart");
        if(cart != null){
                String toUrl = "/order.jsp";
                for (Map.Entry<Commodity,Integer> entry: cart.entrySet()){
                   price = entry.getKey().getPrice();
                   commodityId = entry.getKey().getId();
                   commodityNumber = entry.getValue();
//                   运费默认为商品数乘5
                   freight = commodityNumber*5;
                   consigneeName = orderMassage.get("name");
                   consigneeAddress = orderMassage.get("address");
                   consigneePAC = orderMassage.get("PAC");
                   consigneePhone = orderMassage.get("phone");
                   consigneeFax = orderMassage.get("fax");
                   cardType = orderMassage.get("card_type");
                   carName = orderMassage.get("card_name");
                   cardNumber = orderMassage.get("card_number");
                   billAddress = orderMassage.get("bill_address");
                   closingDate = orderMassage.get("closing_date");;
                   order = new Order
                           (customerEmail,commodityId,commodityNumber,consigneeName,consigneeAddress,consigneePAC,consigneePhone,consigneeFax,cardType,carName,cardNumber,billAddress,closingDate,taxRate,freight,price);
                    boolean check = OrderDao.insertOrder(order);
                    if(check){
                        req.getSession().removeAttribute("cart");
                        req.getSession().removeAttribute("orderMassage");
                        req.getSession().removeAttribute("FinalPrice");
                        toUrl = "/Cart.jsp";
                    }
                }
            resp.sendRedirect(req.getContextPath()+toUrl);
        }
    }


    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
